package com.jesterplayer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jesterplayer.fragment.GenreFragment;
import com.jesterplayer.fragment.TracksFragment;


public class LibraryActivity extends SherlockFragmentActivity {
	
	 public final static String[] GENRES_DB = {
         "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop",
         "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock",
         "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
         "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance",
         "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise",
         "AlternRock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
         "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic",
         "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",
         "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
         "New Wave", "Psychedelic", "Rave", "Showtunes", "Trailer", "Lo-Fi", "Tribal",
         "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock",
         "Folk", "Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin",
         "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock",
         "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus",
         "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music",
         "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam",
         "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul",
         "Freestyle", "Duet", "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
         "Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie", "Britpop",
         "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal",
         "Black Metal", "Crossover", "Contemporary Christian", "Christian Rock ", "Merengue",
         "Salsa", "Thrash Metal", "Anime", "JPop", "Synthpop"
 };
	
	

	/**
	 * The {@link android.support.v4.view.PagerAdapter} that will provide
	 * fragments for each of the sections. We use a
	 * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
	 * will keep every loaded fragment in memory. If this becomes too memory
	 * intensive, it may be best to switch to a
	 * {@link android.support.v4.app.FragmentStatePagerAdapter}.
	 */
	//SectionsPagerAdapter mSectionsPagerAdapter;

	/**
	 * The {@link ViewPager} that will host the section contents.
	 */
	private ViewPager mViewPager;
	private static int currentPage;
	private static String T = "Wtf";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
		super.onCreate(savedInstanceState);
		
		// Apollo sources
        // Scan for music
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        // Control Media volume
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
		
		setContentView(R.layout.activity_library);
		
		mViewPager = (ViewPager) findViewById(R.id.pager);
		
		ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);
        bar.setDisplayHomeAsUpEnabled(false);
        
        
        // Attach tab adapters to the tabs
        TabsAdapter mTabsAdapter = new TabsAdapter(this, mViewPager);
        Bundle bun = new Bundle();
        
        // Adding tabs
        mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_title_genre),
                GenreFragment.class, bun);
/*        mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_title_album),
                AlbumFragment.class, bun);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_title_artist),
                ArtistFragment.class, bun);*/
        mTabsAdapter.addTab(bar.newTab().setText(R.string.tab_title_song),
                TracksFragment.class, bun);
        
        // Bottom Fragment, placeholder now
        Button container = (Button) findViewById(R.id.library_text);
        container.setText("Back");
        container.setVisibility(View.VISIBLE);
        
        //This Listener will be added to the bottom bar and will
        //navigate away from this page.
        final Activity thisActivity = this;
        container.setOnClickListener(new OnClickListener() 
        {

			@Override
			public void onClick(View v) 
			{
				// TODO Auto-generated method stub
				NavUtils.navigateUpFromSameTask(thisActivity);
			}
		});
        
        Button prev = (Button) findViewById(R.id.prev_button);
        Button next = (Button) findViewById(R.id.next_button);
        
        prev.setText("<<");
        next.setText(">>");
        
        prev.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PlaybackController.previousSong();
			}
		});
        next.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PlaybackController.nextSong();
			}
		});
	}
	
	public void onPause() {
		super.onPause();
		currentPage = mViewPager.getCurrentItem();
		overridePendingTransition(R.animator.activity_pull_up,
				R.animator.activity_push_up);
	}
	
	public void onResume() {
		super.onResume();
		mViewPager.setCurrentItem(currentPage);
		overridePendingTransition(R.animator.activity_pull_down,
				R.animator.activity_push_down);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            overridePendingTransition(R.animator.activity_pull_up, 
            		R.animator.activity_push_up);
            return true;
        }
        return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		
		overridePendingTransition(R.animator.activity_pull_up,
				R.animator.activity_push_up);
	}
	
	// Code from stackoverflow
	public static class TabsAdapter extends FragmentPagerAdapter implements
			ActionBar.TabListener, ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final ActionBar mActionBar;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mActionBar = activity.getSupportActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		public int getCount() {
			return mTabs.size();
		}

		public SherlockFragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return (SherlockFragment) Fragment.instantiate(mContext,
					info.clss.getName(), info.args);
		}

		public void onPageSelected(int position) {
			mActionBar.setSelectedNavigationItem(position);
		}

		public void onPageScrollStateChanged(int state) {
		}

		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			mViewPager.setCurrentItem(tab.getPosition());

			Object tag = tab.getTag();
			for (int i = 0; i < mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
					mViewPager.setCurrentItem(i);
				}
			}
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}

		public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {
		}

		public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
		}
	}
}